package Service;

import DAO.SqlHelper;
import Model.News;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewService {
    public  List<News> FindAllNews(){
        String sql = "select * from News";
        ResultSet rs= SqlHelper.executeQuery(sql);
        List<News> newList=new ArrayList();
        try {
            while(rs.next()){
                News n=new News();
                n.setid(rs.getInt("id"));
                n.setTitle(rs.getString("title"));
                n.setContent(rs.getString("content"));
                n.setAuthor(rs.getString("author"));
                newList.add(n);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  newList;

    }
    public Boolean AddNews(News news){
        String sql=String.format("insert into news(title,content) values('%s','%s')",news.getTitle(),news.getContent());
        Boolean result= SqlHelper.executeUpdate(sql);
        if(result) return true;
        else return false;
    }
    public Boolean DeleteNews(String newID){
        String sql="delete from News where id="+newID;
        Boolean result= SqlHelper.executeUpdate(sql);
        if(result) return true;
        else return false;

    }
    public Boolean UpdateNews(News news){return true;}
    public News FindNewsByID(int newID){
        return null;
    }
}
